package io.gbznt.modules.buss.quartz;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import com.serotonin.modbus4j.BatchResults;
import com.serotonin.modbus4j.base.ModbusUtils;
import com.serotonin.modbus4j.exception.ErrorResponseException;
import com.serotonin.modbus4j.exception.ModbusInitException;
import com.serotonin.modbus4j.exception.ModbusTransportException;

import io.gbznt.modules.buss.entity.BussSwitchEntity;
import io.gbznt.modules.buss.entity.BussWeatherRecordEntity;
import io.gbznt.modules.buss.modbus.Modbus4jUtils;
import io.gbznt.modules.buss.service.BussSwitchService;
import io.gbznt.modules.buss.service.BussWeatherRecordService;
import net.sf.json.JSONObject;

public class MyJobswitch implements Job {
	@Autowired
    private BussSwitchService bussWeatherRecordService;
	
	String cityid;
	URLConnection connectionData;
	StringBuilder sb;
	BufferedReader br;
	JSONObject jsonData;
	JSONObject info;
    @Override
	public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
    	 JobDataMap dataMap = jobExecutionContext.getJobDetail().getJobDataMap();
         
 		String name = dataMap.getString("parkey");
    	
    	 new Thread(new MyRuns(name)).start();
    	
    }
    
    public MyJobswitch() {
        //new的时候注入需要的bean
        this.bussWeatherRecordService = ApplicationContextProvider.getBean(BussSwitchService.class);
    }
    
	/**
	 * 线程执行
	 * 
	 * @author Administrator
	 *
	 */
	class MyRuns implements Runnable {
        String key ;
		
		public MyRuns(String name) {
			super();
			key = name;
		}

		@Override
		public void run() {
			// TODO Auto-generated method stub
			try {
				BatchResults<Integer> results = Modbus4jUtils.batchReaddx();
				System.out.print(results);
				short switch1 = (short) results.getValue(0);
				short switch2 = (short) results.getValue(1);
				short switch3 = (short) results.getValue(2);
				short switch4 = (short) results.getValue(3);
				short switch5 = (short) results.getValue(4);
				short switch6 = (short) results.getValue(5);
				short switch7 = (short) results.getValue(6);
				short switch8 = (short) results.getValue(7);
				short switch9 = (short) results.getValue(8);
				short switch10 = (short) results.getValue(9);
				short switch11 = (short) results.getValue(10);
				short switch12 = (short) results.getValue(11);
				short switch13 = (short) results.getValue(12);
				short switch14 = (short) results.getValue(13);
				short switch15 = (short) results.getValue(14);
				short switch16 = (short) results.getValue(15);
				short switch17 = (short) results.getValue(16);
				short switch18 = (short) results.getValue(17);
				short switch19 = (short) results.getValue(18);
				short switch20 = (short) results.getValue(19);
				
				BussSwitchEntity be = new BussSwitchEntity();
				
				be.setSwitch1((int)switch1);
				be.setSwitch2((int)switch2);
				be.setSwitch3((int)switch3);
				be.setSwitch4((int)switch4);
				be.setSwitch5((int)switch5);
				be.setSwitch6((int)switch6);
				be.setSwitch7((int)switch7);
				be.setSwitch8((int)switch8);
				be.setSwitch9((int)switch9);
				be.setSwitch10((int)switch10);
				be.setSwitch11((int)switch11);
				be.setSwitch12((int)switch12);
				be.setSwitch13((int)switch13);
				be.setSwitch14((int)switch14);
				be.setSwitch15((int)switch15);
				be.setSwitch16((int)switch16);
				be.setSwitch17((int)switch17);
				be.setSwitch18((int)switch18);
				be.setSwitch19((int)switch19);
				be.setSwitch20((int)switch20);
				be.setCreateTime(new Date());
				bussWeatherRecordService.save(be);
				
				
			} catch (ModbusTransportException | ErrorResponseException | ModbusInitException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
	}
    
    
}